package com.java2007.hotel.dao.impl;

import com.java2007.hotel.pojo.DetailOrder;
import com.java2007.hotel.dao.DetailOrderDao;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 订单详情实体类
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
public class DetailOrderDaoImpl implements DetailOrderDao {
    /**
     * 根据订单编号查询
     * @param orderNumber
     * @return
     */
    @Override
    public List<DetailOrder> selectDetailOrderByOrder(String orderNumber) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  detail_id id,\n" +
                "  detail_food_id foodId,\n" +
                "  detail_food_price foodPrice,\n" +
                "  detail_food_count foodCount,\n" +
                "  detail_food_total_price foodTotalPrice,\n" +
                "  detail_food_create_date createDate,\n" +
                "  detail_food_update_date updateDate,\n" +
                "  order_number orderNumber\n" +
                "FROM t_order_detail\n" +
                "WHERE order_number = ?";
        List<DetailOrder> query = queryRunner.query(sql, new BeanListHandler<DetailOrder>(DetailOrder.class), orderNumber);
        return query;
    }

    /**
     * 根据菜品id查询菜单详情
     * @param foodId
     * @return
     */
    @Override
    public List<DetailOrder> selectByFoodId(int foodId) throws SQLException {

        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  detail_id id,\n" +
                "  detail_food_id foodId,\n" +
                "  detail_food_price foodPrice,\n" +
                "  detail_food_count foodCount,\n" +
                "  detail_food_total_price foodTotalPrice,\n" +
                "  detail_food_create_date createDate,\n" +
                "  detail_food_update_date updateDate,\n" +
                "  order_number orderNumber\n" +
                "FROM t_order_detail\n" +
                "WHERE detail_food_id = ?";
        List<DetailOrder> query = queryRunner.query(sql, new BeanListHandler<DetailOrder>(DetailOrder.class), foodId);
        return query;
    }

    /**
     * 菜单详情中增加一个信息
     * @param detailOrder
     * @return
     */
    @Override
    public int addDetailOrder(DetailOrder detailOrder,int createrId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "INSERT INTO t_order_detail\n" +
                "            (detail_food_id,\n" +
                "             detail_food_price,\n" +
                "             detail_food_count,\n" +
                "             detail_food_total_price,\n" +
                "             detail_food_create_date,\n" +
                "             detail_food_update_date,\n" +
                "             creater_id)\n" +
                "VALUES (?,?,?,?,?,?,?);";

        int update = queryRunner.update(JdbcUtils.getConnection(), sql, detailOrder.getFoodId(), detailOrder.getFoodPrice(),
                detailOrder.getFoodCount(), detailOrder.getFoodTotalPrice(), detailOrder.getCreateDate(),
                detailOrder.getUpdateDate(), createrId);
        return update;
    }

    /**
     * 修改菜单详情中数量
     * @param foodCount
     * @return
     */
    @Override
    public int updateFoodCount(Date updateDate, int foodCount,Long foodTotalPrice ,int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "UPDATE t_order_detail\n" +
                "SET detail_food_count = ?,\n" +
                "  detail_food_total_price = ?,\n" +
                "  detail_food_update_date = ? where detail_id = ?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql,foodCount,foodTotalPrice,updateDate,id);
        return update;
    }
    /**
     * 查询某用户为下单的菜单详情
     * @param id
     * @return
     */
    @Override
    public List<DetailOrder> selectDetailOrderByUserIdAndUnOrder(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  detail_id id,\n" +
                "  detail_food_id foodId,\n" +
                "  detail_food_price foodPrice,\n" +
                "  detail_food_count foodCount,\n" +
                "  detail_food_total_price foodTotalPrice,\n" +
                "  detail_food_create_date createDate,\n" +
                "  detail_food_update_date updateDate,\n" +
                "  order_number orderNumber\n" +
                "FROM t_order_detail\n" +
                "WHERE creater_id = ? and order_number IS null";
        List<DetailOrder> query = queryRunner.query(sql, new BeanListHandler<DetailOrder>(DetailOrder.class), id);
        return query;
    }
    /**
     * 根据id删除订单详情
     * @param id
     * @return
     */
    @Override
    public int deleteDetailOrderById(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "DELETE FROM t_order_detail WHERE detail_id = ?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, id);
        return update;
    }

    /**
     * 根据id查询菜单详情
     * @param id
     * @return
     */
    @Override
    public DetailOrder selectById(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  detail_id id,\n" +
                "  detail_food_id foodId,\n" +
                "  detail_food_price foodPrice,\n" +
                "  detail_food_count foodCount,\n" +
                "  detail_food_total_price foodTotalPrice,\n" +
                "  detail_food_create_date createDate,\n" +
                "  detail_food_update_date updateDate,\n" +
                "  order_number orderNumber\n" +
                "FROM t_order_detail\n" +
                "WHERE detail_id = ?";
        DetailOrder query = queryRunner.query(sql, new BeanHandler<>(DetailOrder.class), id);
        return query;
    }

    /**
     * 根据订单编号查询菜单详情
     * @param foodNumber
     * @return
     */
    @Override
    public List<DetailOrder> selectByFoodNumber(String foodNumber) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  detail_id id,\n" +
                "  detail_food_id foodId,\n" +
                "  detail_food_price foodPrice,\n" +
                "  detail_food_count foodCount,\n" +
                "  detail_food_total_price foodTotalPrice,\n" +
                "  detail_food_create_date createDate,\n" +
                "  detail_food_update_date updateDate,\n" +
                "  order_number orderNumber\n" +
                "FROM t_order_detail\n" +
                "WHEREorder_number = ?";
        List<DetailOrder> query = queryRunner.query(sql, new BeanListHandler<DetailOrder>(DetailOrder.class), foodNumber);
        return query;
    }

    /**
     * 根据创建人添加菜单详情的订单编号
     * @param createrId
     * @return
     */
    @Override
    public int updateOrderNumber(int createrId, String numer) throws SQLException {
        String sql = "UPDATE t_order_detail SET order_number = ? WHERE creater_id =? and order_number IS null";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, numer, createrId);
        return update;
    }

    /**
     * 根据编号删除订单详情
     * @param number
     * @return
     */
    @Override
    public int deleteDetailOrderByNumber(String number) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "DELETE FROM t_order_detail WHERE order_number = ?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, number);
        return update;
    }


}
